package todlu.RssReader.SinaWeibo;

import weibo4android.WeiboException;
import weibo4android.http.AccessToken;
import weibo4android.http.RequestToken;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;

public class SinaTokenStore {
	private static final String FILE_NAME = "Sina_token";
	
	public static void store(Activity activity, String url)
	{
		try 
		{
			RequestToken requestToken= OAuthConstant.getInstance().getRequestToken();
			Uri uri = Uri.parse(url);
			AccessToken accessToken;
			accessToken = requestToken.getAccessToken(uri.getQueryParameter("oauth_verifier"));
			OAuthConstant.getInstance().setAccessToken(accessToken);
			
			String token = accessToken.getToken();
			String secret = accessToken.getTokenSecret();
			
			SharedPreferences settings = activity.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
			
			SharedPreferences.Editor editor = settings.edit();
			editor.putString("oauth_token", token);
			editor.putString("oauth_token_secret", secret);	
			
			editor.commit();
			
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static boolean fetch(Activity activity)
	{
		SharedPreferences settings = activity.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		
		String oauth_token = settings.getString("oauth_token", null);
		String oauth_token_secret = settings.getString("oauth_token_secret", null);
		
		if(oauth_token != null && oauth_token_secret != null)
		{
			OAuthConstant.getInstance().setToken(oauth_token);
			OAuthConstant.getInstance().setTokenSecret(oauth_token_secret);
			return true;
		}
		else
		{
			return false;
		}
		
		
		
		
		
	}
	
}
